#include <stdio.h>
#include <stdlib.h>

#include "BEncode.h"
#include "SHA1Digest.h"

int main()
{
	FILE* fp = fopen("test4.torrent", "rb");
	if(fp == nullptr)
	{
		printf("file open err\n");
		return -1;
	}
	char* buffer = new char[1024 * 1024];
	int len = fread(buffer, 1, 1024 * 1024, fp);
	if(len <= 0)
	{
		printf("file size:%d\n", len);
		fclose(fp);
		delete[] buffer;
		return -1;
	}
	BEncode* encode = new BEncode;
	BItem* item = encode->load(buffer, len);
	if(item == nullptr)
	{
		printf("torrent format err\n");
		delete[] buffer;
		return -1;
	}
	encode->print();
	delete[] buffer;

	FILE* f = fopen("video.bin", "rb");
	if (f == nullptr)
	{
		printf("file video.bin open err\n");
		return -1;
	}
	char* v = new char[1024 * 1024];
	fread(v, 1, 1024 * 1024, f);
	SHA1Digest sha1;

	for (size_t i = 0; i < 8; i++)
	{
		sha1.Reset();
		sha1.Update(v + i * 131072, 131072);
		printf("sha1:%s\n", sha1.GetString().c_str());
	}

	delete[] v;
	return 0;
}
